package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.utils.StringUtils;
import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.UserModel;
import com.jfinal.plugin.activerecord.Page;
/**
 * 用户业务
 * @author zhaokf
 * @date 2013-04-15
 */
public class UserService extends BaseService{
	
	
	/**
	 * 用户登录 New
	 * @param account
	 * @param password
	 * @return
	 */
	public static UserModel login(Map<String,String> params){
		UserModel users = null;
		String account = null;
		String password = null;
		try{
			account = params.get("account")==null?"":params.get("account").toString();
			password = params.get("password")==null?"":params.get("password").toString();
			
			String sql = "SELECT * FROM SA_USER WHERE USER_LOGINCODE=? AND USER_LOGINPW=?";
			String md5password = StringUtils.MD5(password);
			users = UserModel.dao.findFirst(sql, account,md5password);
		}catch (Exception e){
			users = null;
		}
		return users;
	}

	/**
	 * 分页列出
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<UserModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("user_no") != null){
				where = " AND user_no=" + search.get("user_no");
			}
			if (search.get("user_name") != null){
				where = " AND user_name LIKE '%" + search.get("user_name")+ "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}
		Page<UserModel> Users = null;
		String select_sql = "SELECT * ";
		String query_sql = "FROM SA_USER " + where;
		try{
		    Users = UserModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return Users;
	}

	/**
	 * 增加用户信息
	 * @param no
	 * @param name
	 * @param gender
	 * @param org
	 * @param dept
	 * @param job
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){
		
		String account = params.get("account")==null?"":params.get("account").toString();
		String password = params.get("password")==null?"":params.get("password").toString();
		String no = params.get("user_no")==null?"":params.get("user_no").toString();
		String name = params.get("user_name")==null?"":params.get("user_name").toString();
		String gender = params.get("user_gender")==null?"":params.get("user_gender").toString();
		String org = params.get("user_org_no")==null?"":params.get("user_org_no").toString();
		String dept = params.get("user_dept_no")==null?"":params.get("user_dept_no").toString();
		String job = params.get("user_job_no")==null?"":params.get("user_job_no").toString();
		
		String sql = "select * from sa_user where user_loginCode=?";
		UserModel um = UserModel.dao.findFirst(sql, account);
		if (um == null){
			um = new UserModel();
			um.set("user_no", no);
			um.set("user_name", name);
			um.set("user_gender", gender);
			um.set("user_org_no", org);
			um.set("user_dept_no", dept);
			um.set("user_job_no", job);
			um.set("user_loginCode", account);
			/**当保存密码的时候需要MD5加密（）**/
			um.set("user_loginPW", StringUtils.MD5(password));
			um.save();
			return true;
		}
		return setError("你访问的用户信息已存在！");
	}

	/**
	 * 修改用户信息
	 * @param no
	 * @param name
	 * @param gender
	 * @param org
	 * @param dept
	 * @param job
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		
		String no = params.get("user_no")==null?"":params.get("user_no").toString();
		String name = params.get("user_name")==null?"":params.get("user_name").toString();
		String gender = params.get("user_gender")==null?"":params.get("user_gender").toString();
		String org = params.get("user_org_no")==null?"":params.get("user_org_no").toString();
		String dept = params.get("user_dept_no")==null?"":params.get("user_dept_no").toString();
		String job = params.get("user_job_no")==null?"":params.get("user_job_no").toString();
		UserModel um = UserModel.dao.findById(no);
		if (um != null){
			um.set("user_no", no);
			um.set("user_name", name);
			um.set("user_gender", gender);
			um.set("user_org_no", org);
			um.set("user_dept_no", dept);
			um.set("user_job_no", job);
			um.update();
			return true;
		}
		return setError("您访问的用户信息不存在！");
	}

	/**
	 * 删除用户信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("user_no")==null?"":params.get("user_no").toString();
		UserModel um = UserModel.dao.findById(no);
		if (um != null){
			um.delete();
			return true;
		}
		return setError("您访问的用户信息不存在！");
	}
}
